<?php

namespace app\index\model;

use think\Model;

class Notice extends Model
{
    // 入口  通知客户端下载图片 
    public function process ( $info=[] ){

        if ( !$this->check_remote_file_exists( $info['file'] ) ) {
            return true ;
        }

        // rpc 测试
        // http://www.fractal.com
        // print_r(['rpc',$info]);
        $file       = $info['file'] ;
        $ffprobe    = \FFMpeg\FFProbe::create();
        $durations  = $ffprobe->format($file);

        $client = new \Hprose\Http\Client( $info['web_url'].'/mdresource/rpcHprose',false );

        // 视频
        if ( $info['type']=='Video' ) {

            $duration   = $durations->get('duration');
            // 视频
            $videos     = $ffprobe->streams( $file )->videos()->first();
            $width      = $videos->get('width');
            $height     = $videos->get('height');

            $result = $client->init('update_video', [
                $info['files'] ,
                $info['item_id'] ,
                [
                    'other' => [
                        'duration'  => $duration ,
                        'width'     => $width ,
                        'height'    => $height ,
                        'imgs'      => [
                            $info['img']
                        ] ,
                    ]
                ] ,
                $info['file'] ,
                $info['zh'] ,
            ]);
        }

        // 音频
        if ( $info['type']=='Audio' ) {

            $duration   = $durations->get('duration');

            $result = $client->init('update_audio', [
                $info['files'] ,
                $info['item_id'] ,
                [
                    'other' => [
                        'duration'  => $duration ,
                    ]
                ] ,
                $info['file']
            ]);
        }

        // print_r(['rpc',$result]);

        return true ;
    }

    // 检查远程文件是否存在
    public function check_remote_file_exists($url) {
        // if ( !$this->check_remote_file_exists( $data['file'] ) ) {
        //     exception($data['item_id'].'任务进行中');
        // }
        $curl = curl_init($url); // 不取回数据
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // 发送请求
        $result = curl_exec($curl);
        $found = false; // 如果请求没有发送失败
        if ($result !== false) {
            /** 再检查http响应码是否为200 */
            $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($statusCode == 200) {
                $found = true;
            }
        }
        curl_close($curl);
        return $found;
    }

}
